package com.henry.newscafe;

import java.util.ArrayList;
import java.util.List;

public class Newspaper {
    private String name;
    private List<String> articles;
    private List<Reader> readers;

    Newspaper(String name) {
        this.name = name;
        articles = new ArrayList<String>();
        readers = new ArrayList<Reader>();
    }

    public String getName() {
        return this.name;
    }

    // add the article to the articles list and notify all readers about this update
    void addArticle(String article) {
        this.articles.add(article);
        this.notifyAllReader();
    }

    // get some article by its number
    String getNewestArticle() {
        return articles.get(articles.size() - 1);
    }

    // add a new reader to the readers list
    void addReader(Reader someReader) {
        readers.add(someReader);
    }

    // remove a reader from the readers list
    void removeReader(Reader someReader) {
        readers.remove(someReader);
    }

    // let all readers who subscribe this newspaper read this article
    void notifyAllReader() {
        for (Reader reader : readers) {
            reader.readArticle(this);
        }
    }
}
